﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Yelper.Web {

    public static class PrettyYelpHelper {

        public static MvcHtmlString PrettyYelp(this HtmlHelper helper, string yelp) {

            string output = string.Empty;

            string[] tokens = yelp.Split(' ');
            foreach (string token in tokens) {
                if (token.StartsWith("@")){
                    output += "<a href=\"/" + token.Substring(1) + "\">" + token + "</a> ";
                } else if (token.StartsWith("λ")) {
                    output += "<a href=\"/Search/" + token.Substring(1) + "\">" + token + "</a> ";
                } else {
                    output += Linkify(token) + " ";
                }
            }

            return new MvcHtmlString(output.TrimEnd());
        
        }

        private static string Linkify(string input) {
            if (input.StartsWith("http://") || input.StartsWith("https://")) {
                return "<a href=\"" + input + "\">" + (input.Length > 56 ? input.Substring(0,55) + "..." : input) + "</a> ";
            } else {
                return (input.Length > 56 ? input.Substring(0, 55) + "..." : input);
            }
        }

    }

}
